#define ADRENALIN_EXPORTS

#include "../include/Log.h"
//
Adrenalin::Log* Adrenalin::Singleton<Adrenalin::Log>::_pInstance = NULL;

namespace Adrenalin
{
	// ------------------------------------------------------------------------
	// Constructor
	// ------------------------------------------------------------------------
	Log::Log()
	{
		this->_fileName = L"_AdrenalinLog.txt";
	}
	// ------------------------------------------------------------------------
	// Function: Init
	// Description: called to create a Logfile
	// ------------------------------------------------------------------------
	void	Log::Init(Adrenalin::stringw file)
	{
		_fileName = file;
		//Create new File
		_logFile.open(_fileName.c_str(), std::ios::out);
	}
	// ------------------------------------------------------------------------
	// Function: Close
	// Description: called to close Logfile
	// ------------------------------------------------------------------------
	void	Log::Close()
	{
		_logFile.close();
	}
	// ------------------------------------------------------------------------
	// Function: Write
	// Description: Writes a Message to Logfile
	// ------------------------------------------------------------------------
	void	Log::Write(const wchar_t* message, ELogType type)
	{
		if(_logFile.is_open()) {
			_logFile << Time().c_str() << " (" << ConvertType(type).c_str() << "): \t" << message << std::endl;
		}
	}// Write
	// ------------------------------------------------------------------------
	// Function: Write
	// Description: Writes a Message to Logfile
	// ------------------------------------------------------------------------
	void	Log::Write(const wchar_t* message, ELogType type, const Adrenalin::stringw& file, const Adrenalin::stringw& function, const unsigned long line)
	{
		if(_logFile.is_open()) {
			_logFile << Time().c_str() << " (" << ConvertType(type).c_str() << "): \t" << message;
			_logFile << " (File: " << file.c_str() << " Function: " << function.c_str() << " Line: " << line << ")" << std::endl;
		}
	}// Write
	// ------------------------------------------------------------------------
	// Function: Write
	// Description: Writes a Message to Logfile
	// ------------------------------------------------------------------------
	void	Log::Write(const Adrenalin::stringw& message, ELogType type, const Adrenalin::stringw& file, const Adrenalin::stringw& function, const unsigned long line)
	{
		if(_logFile.is_open()) {
			_logFile << Time().c_str() << " (" << ConvertType(type).c_str() << "): \t" << message.c_str();
			_logFile << " (File: " << file.c_str() << " Function: " << function.c_str() << " Line: " << line << ")" << std::endl;
		}
	}// Write
	// ------------------------------------------------------------------------
	// Function: Time
	// Description: Returns a string with current time
	// ------------------------------------------------------------------------
	Adrenalin::stringw Log::Time()
	{
		SYSTEMTIME st;
		wchar_t date[128];
		wchar_t time[128];
		GetLocalTime (&st);
		GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, L"dd'.'MM'.'yyyy", date,
				128);
		GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, L"HH':'mm':'ss", time,
				128);

		return Adrenalin::stringw(time);// + Adrenalin::stringw(L" - ") + Adrenalin::stringw(date);
	}
	// ------------------------------------------------------------------------
	// Function: ConvertType
	// Description: converts ELogType to string
	// ------------------------------------------------------------------------
	Adrenalin::stringw Log::ConvertType(ELogType type)
	{
		Adrenalin::stringw result;

		switch(type)
		{
		case LOG_INFO:
			result = L"Info";
			break;
		case LOG_WARNING:
			result = L"Warning";
			break;
		case LOG_ERROR:
			result = L"Error";
			break;
		case LOG_EXCEPTION:
			result = L"Exception";
			break;
		case LOG_DEBUG:
			result = L"Debug";
			break;
		default:
			result = L"None";
			break;
		}
		return result;
	}
}